<?php

namespace app\app\controller;


use app\app\model\ProjectModel;
use app\app\model\ProjectPoliticalModel;
use app\app\model\ProjectZrrDetailModel;
use app\app\model\ProjectZrrModel;
use app\BaseController;


class ProjectPolitical extends BaseController
{
    /**
     * 政处问题添加
     */
    public function add()
    {
        $data = $this->request->param();
//        var_dump($data);die();
        $res = ProjectPoliticalModel::create($data);
        if ($res) {
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }

    /**
     * 政处问题修改
     */
    public function update()
    {
        $data = $this->request->param();
        $id = $data['id'];
        unset($data['id']);
        $res = ProjectPoliticalModel::where('id', $id)->update($data);
        if ($res) {
            $this->success('成功');
        }
        $this->error('失败');
    }


    /**
     * 根据项目获取政处问题列表
     */
    public function index()
    {
        $project_id = input('project_id', '');
        $page_size = input('pageSize', '20');
        $list = ProjectPoliticalModel::alias('a')->field('a.*,b.file_name,b.file_path')
            ->leftjoin('sys_file_info b', 'b.file_id = a.file_id')
            ->where('a.project_id', $project_id)
            ->where('state', '<>', '3')->paginate($page_size);
        $this->success('success', $list);
    }

    /**
     * 获取政处问题主体项目
     */
    public function list()
    {
        $where[] = ['c.state', '<>', 3];
        $list = ProjectModel::alias('a')
            ->field('b.project_name as project_main_name,b.id as main_id')
            ->join('t_project b', 'a.link_project_id = b.id')
            ->join('t_project_political c', 'a.id = c.project_id')
            ->group('b.project_name')
            ->where($where)
            ->select();
        $this->success('success', $list);
    }

    /**
     * 根据主体项目id获取单体政处问题项目
     */
    public function politicalList()
    {
        $id = input('project_id', '');
        $where[] = ['b.link_project_id', '=', $id];
        $where[] = ['a.state', '<>', 3];
        $list = ProjectPoliticalModel::alias('a')
            ->field('b.id,b.project_name,COUNT(a.project_id) as total')
            ->leftjoin('t_project b', 'a.project_id = b.id')
            ->leftjoin('t_project c', 'c.link_project_id = b.id')
            ->where($where)
            ->group('c.project_name')
            ->select();
        $this->success('success', $list);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function politicals()
    {
        $project_id = input('project_id', '');
        $list = ProjectPoliticalModel::alias('a')->field('a.*,b.file_name,b.file_path')
            ->leftjoin('sys_file_info b', 'b.file_id = a.file_id')
            ->where('a.project_id', $project_id)
            ->where('state', '<>', '3')->select();

        $this->success('success', $list);
    }

    /**
     * 根据政处id获取详情
     */
    public function detail()
    {
        $id = input('id', '');
        $list = ProjectPoliticalModel::alias('a')->field('a.*,b.file_name,b.file_path')
            ->leftjoin('sys_file_info b', 'b.file_id = a.file_id')
            ->where('a.id', $id)->find();
        $this->success('success', $list);
    }


    /**
     * 根据项目获取责任单位
     */
    public function company()
    {
        $project_id = input('project_id', '');
        $where[] = ['dw_type', '<>', '业主单位'];
        $where[] = ['project_id', '=', $project_id];
        $list = ProjectZrrModel::where($where)->select();
        $this->success('success', $list);
    }

    /**
     * 获取单位下的人员信息
     */
    public function companyPeople()
    {
        $company_id = input('company_id', '');
        $where[] = ['link_id', '=', $company_id];
        $where[] = ['status', '=', 1];
        $list = ProjectZrrDetailModel::field('id,zrr_name')->where($where)->select();
        $this->success('success', $list);
    }

}